<!-- 在线问诊页面 -->
<template>
  <div class="zxwz-container">
    <!-- 搜索框 -->
    <div class="search-bar">
      <img src="/static/UtilsImage/Search.png" class="search-icon" />
      <input class="search-input" placeholder="输入医生/疾病搜索" v-model="searchValue" />
    </div>

    <!-- 专家推荐 -->
    <div class="section-title-highlight">
      <span class="section-title-text">专家推荐</span>
      <div class="section-title-underline"></div>
    </div>
    <div class="expert-list">
      <div class="expert-card" v-for="i in GoodDoctorList" :key="i">
        <img :src="i.avatar" class="expert-avatar" />
        <div class="expert-name">{{ i.name || '--' }}</div>
        <div class="expert-title">{{ i.d_title || '--' }}</div>
      </div>
    </div>

    <!-- 筛选栏 -->
    <div class="filter-bar">
      <div class="filter-item" @click="openHospitalList">
        <span :class="{ active: showHospitalList }">{{ selectedHospital }}</span>
        <span class="arrow-down">▼</span>
      </div>
      <div class="filter-item" @click="openDeptList">
        <span :class="{ active: showDeptList }">{{ selectedDept }}</span>
        <span class="arrow-down">▼</span>
      </div>
    </div>
    <div v-if="showHospitalList" class="dropdown-list">
      <div
        v-for="item in hospitalList"
        :key="item"
        :class="['dropdown-item', { selected: selectedHospital === item }]"
        @click="selectHospital(item)"
      >
        {{ item.title }}
      </div>
    </div>
    <div v-if="showDeptList" class="dropdown-list">
      <div
        v-for="item in deptList"
        :key="item"
        :class="['dropdown-item', { selected: selectedDept === item }]"
        @click="selectDept(item)"
      >
        {{ item.title }}
      </div>
    </div>
    <div class="hospital-list">
      <template v-if="doctorList.length > 0">
        <div class="doctor-list">
          <div class="doctor-card" v-for="i in doctorList" :key="i">
            <img src="/static/MyImage/avatar.jpg" class="doctor-avatar" />
            <div class="doctor-info">
              <div class="doctor-name">{{ i.name || '--' }}</div>
              <div class="doctor-hospital">{{ i.h_title || '科室' }}</div>
              <div class="doctor-goodat">
                擅长：<span class="goodat-keyword">{{ i.good_at || '擅长' }}</span>
              </div>
            </div>
            <button class="consult-btn">
              <img src="/static/NavigateImage/Xiaoxi.png" class="consult-icon" /> 咨询
            </button>
          </div>
        </div>
      </template>
    </div>
  </div>
  <!-- 医生列表
  <div class="doctor-list">
      <div class="doctor-card" v-for="i in 3" :key="i">
        <img src="/static/MyImage/avatar.jpg" class="doctor-avatar" />
        <div class="doctor-info">
          <div class="doctor-name">医生姓名</div>
          <div class="doctor-hospital">XXXXXX医院</div>
          <div class="doctor-goodat">
            擅长：<span class="goodat-keyword">皮肤外科、医疗美容、烧伤外科。</span>
          </div>
        </div>
        <button class="consult-btn">
          <img src="/static/NavigateImage/Xiaoxi.png" class="consult-icon" /> 咨询
        </button>
      </div>
    </div> -->
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import {
  getYyghDoctorByDept,
  getYyghDeptData,
  getYyghDoctorData,
  getAllDoctorListData,
  searchDoctorData,
  getYyghGoodDoctorData
} from '@/https/HomeHTTP'
import { onLoad } from '@dcloudio/uni-app'
import { useMemberStore } from '@/store'
const memberStore = useMemberStore()

const GoodDoctorList = ref([])
const getGoodDoctorList = async () => {
  const res = await getYyghGoodDoctorData()
  console.log(res)
  GoodDoctorList.value = res.data
}

const hospitalList = ref([])
const getHospitalList = async () => {
  const res = await getYyghDoctorData()
  console.log(res)
  hospitalList.value = res.data.result
}
const deptList = ref([])
const getDeptList = async (hid: number) => {
  const res = await getYyghDeptData(hid)
  deptList.value = res.data.result
}
const searchDoc = async (name: string) => {
  const res = await searchDoctorData(name)
  doctorList.value = res.data
}
const doctorList = ref([])
const getDoctorList = async (item: any) => {
  // console.log(item)
  if (selectedHospital.value === '全部医院') {
    const res = await getAllDoctorListData()

    doctorList.value = res.data.result
    console.log(doctorList.value)
  } else {
    const res = await getYyghDoctorByDept(item.did, item.hid)
    doctorList.value = res.data
  }
}

onLoad(async () => {
  if(!memberStore.isLogin) return
  await Promise.all([getHospitalList(), getDoctorList(), getGoodDoctorList()])
})

const selectedHospital = ref('全部医院')
const selectedDept = ref('全部科室')
const showHospitalList = ref(false)
const showDeptList = ref(false)
function selectHospital(item: string) {
  selectedHospital.value = item.title
  showHospitalList.value = false
  getDeptList(item.hid)
}
function selectDept(item: string) {
  console.log(item)
  selectedDept.value = item.title
  showDeptList.value = false
  getDoctorList({ did: item.did, hid: item.hid })
}
function openHospitalList() {
  showHospitalList.value = !showHospitalList.value
  if (showHospitalList.value) showDeptList.value = false
}
function openDeptList() {
  showDeptList.value = !showDeptList.value
  if (showDeptList.value) showHospitalList.value = false
}
const searchValue = ref('')
watch(searchValue, (newVal) => {
  if (newVal.trim() !== '') {
    searchDoc(newVal)
  } else {
    getDoctorList()
  }
})
</script>

<style scoped lang="scss">
.zxwz-container {
  background: #f5f5f5;
  min-height: 100vh;
  font-family: 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
  /* 适配原生导航栏下方留白 */
  padding-top: 0 !important;
}
/* 适配H5和小程序原生导航栏下方的默认留白 */
page, body, html {
  background: #f5f5f5 !important;
}
.search-bar {
  display: flex;
  align-items: center;
  background: #fff;
  border-radius: 12px;
  margin: 18px 16px 0 16px;
  box-shadow: 0 4px 12px 0 #d2f5d2;
  padding: 0 16px;
  height: 48px;
}
.search-icon {
  width: 24px;
  height: 24px;
  margin-right: 8px;
}
.search-input {
  border: none;
  outline: none;
  font-size: 16px;
  flex: 1;
  background: transparent;
  color: #888;
}
.section-title {
  display: flex;
  align-items: center;
  font-size: 18px;
  font-weight: bold;
  color: #666;
  margin: 24px 0 8px 20px;
}
.section-dot {
  width: 18px;
  height: 18px;
  margin-right: 6px;
  opacity: 0;
}
.expert-list {
  display: flex;
  overflow-x: auto;
  padding: 0 0 12px 12px;
  margin-bottom: 8px;
  -ms-overflow-style: none; /* IE and Edge */
  scrollbar-width: none; /* Firefox */
}
.expert-list::-webkit-scrollbar {
  display: none;
}
.expert-card {
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 2px 8px 0 #e6f7e6;
  width: 110px;
  min-width: 110px;
  height: 120px;
  margin-right: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.expert-avatar {
  width: 54px;
  height: 54px;
  border-radius: 50%;
  object-fit: cover;
  margin-bottom: 8px;
}
.expert-name {
  font-size: 16px;
  font-weight: bold;
  color: #444;
}
.expert-title {
  font-size: 14px;
  color: #999;
}
.filter-bar {
  display: flex;
  background: #fff;
  border-radius: 8px 8px 0 0;
  box-shadow: 0 2px 8px 0 #f0f0f0;
  padding: 12px 16px 0 16px;
  position: relative;
  z-index: 2;
}
.filter-item {
  flex: 1;
  text-align: center;
  font-size: 18px;
  color: #bdbdbd;
  font-weight: 500;
  padding: 8px 0;
  cursor: pointer;
  position: relative;
  .active {
    color: #3ec99e;
  }
}
.arrow-down {
  font-size: 12px;
  margin-left: 4px;
}
.dropdown-list {
  position: absolute;
  top: 320px;
  left: 0;
  right: 0;
  margin: 0 16px;
  background: #fff;
  border-radius: 0 0 8px 8px;
  box-shadow: 0 2px 8px 0 #f0f0f0;
  z-index: 10;
  max-height: 320px;
  overflow-y: auto;
  animation: fadeIn 0.2s;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-10px);}
  to { opacity: 1; transform: translateY(0);}
}
.dropdown-item {
  font-size: 17px;
  color: #222;
  padding: 14px 0 14px 18px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  background: #fff;
  &.selected {
    color: #3ec99e;
    font-weight: bold;
    background: #f7f8fa;
  }
  &:last-child {
    border-bottom: none;
  }
}
.doctor-list {
  padding: 0 12px 24px 12px;
}
.doctor-card {
  display: flex;
  align-items: flex-start;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 2px 8px 0 #e6f7e6;
  margin-bottom: 18px;
  margin-top:18px;
  padding: 16px 12px;
  position: relative;
}
.doctor-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  object-fit: cover;
  margin-right: 12px;
}
.doctor-info {
  flex: 1;
  min-width: 0;
}
.doctor-name {
  font-size: 17px;
  font-weight: bold;
  color: #222;
  margin-bottom: 2px;
}
.doctor-hospital {
  font-size: 15px;
  color: #888;
  margin-bottom: 2px;
}
.doctor-goodat {
  font-size: 15px;
  color: #888;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 210px;
  display: block;
}
.goodat-keyword {
  color: #ff6600;
}
.consult-btn {
  background: #00c200;
  color: #fff;
  border: none;
  border-radius: 16px;
  font-size: 14px;
  font-weight: bold;
  padding: 2px 12px 2px 8px;
  display: flex;
  align-items: center;
  position: absolute;
  right: 14px;
  bottom: 14px;
  cursor: pointer;
  box-shadow: 0 2px 8px 0 #b6f5b6;
  height: 30px;
  min-width: 56px;
}
.consult-icon {
  width: 18px;
  height: 18px;
  margin-right: 2px;
}
/* 专家推荐标题高亮样式 */
.section-title-highlight {
  position: relative;
  margin: 24px 0 8px 20px;
  display: inline-block;
  height: 28px;
}
.section-title-text {
  font-size: 15px;
  font-weight: bold;
  font-style: italic;
  color: #33343a;
  z-index: 2;
  position: relative;
}
.section-title-underline {
  position: absolute;
  left: 0;
  bottom: 1px;
  width: 55px;
  height: 9px;
  background: #3ec99e;
  z-index: 1;
  border-radius: 3px;
}
.hospital-list-item{
  font-size: 17px;
  color: #222;
  padding: 14px 14px 14px 18px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  background: #fff;
  text-align: center;
  margin-top: 18px;
  margin-bottom: 18px;
}
</style>
